﻿@{
       Layout = "/Views/Shared/_level1.cshtml";

        if (false)
        {
        <script src="~/LiberUI/Scripts/jquery.js"></script>
        <script src="~/LiberUI/jquery-vsdoc.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Base.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Action.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.js"></script>
    }
}
<title>绑定微信账号</title>

<link href="/SiteUI/MyBiz/BindWechat.css" rel="stylesheet" />

<div class="view">
    <div class="row">
        <div class="col-lg-4 col-lg-offset-4" id="bindTitle">
            <h2>天联云-微信绑定</h2>
            <img id="imgWxTmpQr" src="/SiteUI/Imgs/loading-gif.gif" />
        </div>
    </div>
    <div class="row">
        <div class="col-lg-4 col-lg-offset-4" id="bindTip">
            <div>
                <h4>请使用微信扫描二维码完成绑定</h4>
                <h4 id="ourProduct">天联云协同平台</h4>
            </div>

        </div>
        <div class="col-lg-6 col-lg-offset-3" id="bindOk">
            <div>
                <img src="/SiteUI/Imgs/done-ok.png" />
                <h3>绑定成功</h3>
                <h4>欢迎体验天联云移动协同</h4>
            </div>
        </div>
    </div>
</div>


<script>
    function onLoginByWxEnd(rtn) {
        if (rtn.IsPositive) {
            $.getJSON("/TLB2B/GetUserInfo",null, onGetWxUserInfoEnd)          
        }
        else {
            setTimeout(onLoginByWx, 500);
        }
    };
    function onGetWxUserInfoEnd(rtn){
        if (rtn.IsPositive) {
            var uinfo = JSON.parse(JSON.parse(rtn.Data));
                var openUser = {
                    src : "wechat",
                    name: uinfo.nickname,
                    sex: uinfo.sex,
                    headimg: uinfo.headimgurl,
                    id:uinfo.openid
                }
                $.post("/MyBiz/BindOpenUser", openUser, function (r) {
                    if (r.IsPositive) {
                        $("#bindTip").hide();
                        $("#bindOk").fadeIn();
                        setTimeout(function () { window.history.go(-1); }, 2000);
                    }
                    else {
                        error("绑定失败", ErrFormate(res_sobiz, r.ErrorSrc, r.ErrorCode, r.Error), 5, function () { window.history.go(0); });
                    }
                })
            }
            else {
                setTimeout(function () { $.getJSON("/TLB2B/GetUserInfo", null, onGetWxUserInfoEnd) }, 1000);
            }
    };
    function onLoginByWx() {
        $.getJSON("/TLB2B/IfWxTmpQRScaned?tm="+new Date().getTime(), {}, onLoginByWxEnd);
    };
    function onGetTicketEnd(rtn) {
        //TODO:获得微信临时二维码Ticket,直接从微信服务器获取二维码地址
        rtn = JSON.parse(rtn);
        if (rtn.ticket) {
            $("#imgWxTmpQr").attr("src", "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + encodeURI(rtn.ticket));
            //开始检查用户扫码
            setTimeout(onLoginByWx, 200);
        }
    };

    $(function () {
        //获取微信临时二维码
        $.getJSON("/TLB2B/GetWxTmpQRTicket", {}, onGetTicketEnd);
    })
</script>

